<template>
	<div class="sys-region-container">
		<el-row :gutter="8" style="width: 100%; flex: 1">
			<el-col :span="3" :xs="24">
				<RegionTree ref="regionTreeRef" @node-click="nodeClick" />
			</el-col>

			<el-col :span="21" :xs="24" style="display: flex; flex-direction: column">
				<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
					<el-form :model="state.queryParams" ref="queryForm" :inline="true">
						<el-form-item label="项目">
							<el-input v-model="state.queryParams.roadNo" placeholder="路线编码" clearable />
						</el-form-item>
						<el-form-item label="项目编号">
							<el-input v-model="state.queryParams.roadname" placeholder="路线全称" clearable />
						</el-form-item>
						<el-form-item>
							<el-button-group>
								<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysRegion:page'"> 查询 </el-button>
								<el-button icon="ele-Refresh" @click="resetQuery"> 重置 </el-button>
							</el-button-group>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" icon="ele-Plus" @click="openAddRegion" v-auth="'sysRegion:add'"> 新增 </el-button>
						</el-form-item>
					</el-form>
				</el-card>

				<el-card class="full-table" shadow="hover" style="margin-top: 5px">
					<el-table :data="state.regionData" style="width: 100%" v-loading="state.loading" row-key="id" default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
						<el-table-column prop="name" width="140"  label="路网类型" align="center" show-overflow-tooltip />
						<el-table-column prop="roadname" width="140"  label="路线全称" align="center" show-overflow-tooltip />
						<el-table-column prop="roadNo"  width="140" label="路线编码" align="center" show-overflow-tooltip />
						<el-table-column label="操作"  fixed="right"  align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openEditRegion(scope.row)" v-auth="'sysRegion:update'"> 编辑 </el-button>
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openFIleInfo(scope.row)" > 基础数据上传 </el-button>
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openUploadDialog(scope.row,1)" > 上传平整度 </el-button>
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openUploadDialog(scope.row,2)" > 上传车辙数据 </el-button>
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openUploadDialog(scope.row,3)" > 上传构造深度 </el-button>
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openUploadDialog(scope.row,4)" > 上传路面破损 </el-button>
								<el-button icon="ele-Delete" size="small" text type="danger" @click="delRegion(scope.row)" v-auth="'sysRegion:delete'"> 删除 </el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination
						v-model:currentPage="state.tableParams.page"
						v-model:page-size="state.tableParams.pageSize"
						:total="state.tableParams.total"
						:page-sizes="[10, 20, 50, 100]"
						small
						background
						@size-change="handleSizeChange"
						@current-change="handleCurrentChange"
						layout="total, sizes, prev, pager, next, jumper"
					/>
				</el-card>
			</el-col>
		</el-row>
		<el-dialog v-model="state.dialogUploadVisible" :lock-scroll="false" draggable width="400px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-UploadFilled /> </el-icon>
					<span> 上传文件 </span>
				</div>
			</template>
			<div>
				<el-upload ref="uploadRef" drag :auto-upload="false" :limit="5" :file-list="state.fileList" action="" :on-change="handleChange" accept=".jpg,.png,.bmp,.gif,.txt,.pdf,.xlsx,.docx">
					<el-icon class="el-icon--upload">
						<ele-UploadFilled />
					</el-icon>
					<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
					<template #tip>
						<div class="el-upload__tip">请上传大小不超过 10MB 的文件</div>
					</template>
				</el-upload>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialogUploadVisible = false">取消</el-button>
					<el-button type="primary" @click="uploadFile">确定</el-button>
				</span>
			</template>
		</el-dialog>
		<EditRegion ref="editRegionRef" :title="state.editRegionTitle" @handleQuery="handleQuery"  :regionData="state.regionData"/>
		<EditFIleInfo ref="editFIleInfoRef" :title="state.editfileInfoTitle" @handleQuery="handleQuery"  :regionData="state.regionData" :row="state.row"/>

	</div>
</template>

<script lang="ts" setup name="sysRegion">
import { onMounted, reactive, ref } from 'vue';
import {ElMessageBox, ElMessage, ElNotification, UploadInstance} from 'element-plus';
import RegionTree from '/@/views/system/region/component/regionTree.vue';
import EditRegion from '/@/views/system/region/component/editRegion.vue';
import EditFIleInfo from '/@/views/system/region/component/editFIleInfo.vue';

import {SysFileApi} from '/@/api-services/api';

import { getAPI } from '/@/utils/axios-utils';
import { SysRegionApi } from '/@/api-services/api';
import {SysFile, SysRegion} from '/@/api-services/models';
import { uploadFilesPoject} from "/@/api/region/region";
import MapDragRoute from "/@/views/map/MapDragRoute.vue";

const uploadRef = ref<UploadInstance>();
const editRegionRef = ref<InstanceType<typeof EditRegion>>();
const regionTreeRef = ref<InstanceType<typeof RegionTree>>();
const editFIleInfoRef = ref<InstanceType<typeof EditFIleInfo>>();

const state = reactive({
	loading: false,
	key:0,
	Pid : undefined,
	fileData: [] as Array<SysFile>,
	fileList: [] as any,
	fileName: '',
	regionData: [] as Array<SysRegion>, // 列表数据
	row:"",
	queryParams: {
		id: -1,
		pid: undefined,
		year: undefined,
		roadNo: undefined,
		roadname: undefined,
	},
	tableParams: {
		page: 1,
		pageSize: 20,
		total: 0 as any,
	},
	editRegionTitle: '',
	editfileInfoTitle: '',
	isShowDialog:'',
	dialogUploadVisible:false,
});

onMounted(() => {
	handleQuery();
});


// 通过onChanne方法获得文件列表
const handleChange = (file: any, fileList: []) => {
	state.fileList = fileList;
	console.log('实例',state)
};

// 取消
const cancel = () => {
	state.fileList = [];
	state.isShowDialog = false;
};
// 打开上传页面
const openUploadDialog = (row: any,key:any) => {
	state.fileList = [];
	state.Pid=row.id;
	state.key=key;
	state.dialogUploadVisible = true;
};

// 上传
const uploadFile = async () => {
	const params = new FormData();

	if (state.fileList.length >=1){
		for (let i = 0; i < state.fileList.length; i++) {
			state.fileList[i].pid = state.Pid;
			params.append('files', state.fileList[i].raw);
		}
		params.append('pid',state.Pid);
		params.append('key',state.key);
		await uploadFilesPoject(params);
	}
	handleQuery();
	state.key=0;
	state.Pid=0;
	state.dialogUploadVisible = false;
};

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	let params = Object.assign(state.queryParams, state.tableParams);
	var res = await getAPI(SysRegionApi).apiSysRegionPagePost(params);
	state.regionData = res.data.result?.items ?? [];
	state.tableParams.total = res.data.result?.total;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.id = -1;
	state.queryParams.pid = undefined;
	state.queryParams.name = undefined;
	state.queryParams.code = undefined;
	handleQuery();
};

// 打开新增页面
const openAddRegion = () => {
	state.editRegionTitle = '添加项目';
	editRegionRef.value?.openDialog({ orderNo: 100 });
};

// 打开基础数据上传页面
const openFIleInfo = (row: any) => {
	state.editfileInfoTitle = '基础数据上传';
	state.row=row;
	editFIleInfoRef.value?.openDialog(row);
};

// 打开编辑页面
const openEditRegion = (row: any) => {
	state.editRegionTitle = '编辑行政区域';
	editRegionRef.value?.openDialog(row);
};

// 删除
const delRegion = (row: any) => {
	ElMessageBox.confirm(`确定删除行政区域：【${row.name}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await getAPI(SysRegionApi).apiSysRegionDeletePost({ id: row.id });
			handleQuery();
			// 编辑删除后更新机构数据
			regionTreeRef.value?.initTreeData();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

// 树组件点击
const nodeClick = async (node: any) => {
	state.queryParams.pid = node.id;
	state.queryParams.year = node.year;
	console.log('年度值',node.year)
	console.log('组织ID',node.pid)

	handleQuery();
};

// 同步国家统计局操作
const handlSync = async () => {
	ElMessageBox.confirm('确认同步国家统计局行政区域数据？', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			ElNotification({
				title: '提示',
				message: '后台努力同步中...',
				type: 'success',
				position: 'bottom-right',
			});
			await getAPI(SysRegionApi).apiSysRegionSyncPost({ timeout: 1000 * 60 * 30 });
		})
		.catch(() => {});
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	state.tableParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	state.tableParams.page = val;
	handleQuery();
};
</script>
